﻿using System;
using System.Collections.Generic;
using System.Linq;
using Core.Models.Enums;
using ServiceInterface.InputModels;
using Web.Infrastructure.Types;
using Web.InputModels;

namespace Web.Infrastructure.Helpers
{
    public static class MappingHelper
    {
        public static ChessPieceColor MapColor(UIEnums.Color color)
        {
            switch (color)
            {
                case UIEnums.Color.Black:
                    return ChessPieceColor.Black;
                case UIEnums.Color.White:
                    return ChessPieceColor.White;
            }
            throw new NotImplementedException("Mapping for color " + color + " not found");
        }

        public static NewGameInputModel MapCreateGameInputModel(GamePropertiesInputModel inputModel)
        {
            var newGameInputModel = new NewGameInputModel()
            {
                Player1Color = MapColor(inputModel.Player1Color),
                Player2Color = MapColor(inputModel.Player2Color),
                Player1Difficulty = inputModel.Player1Difficulty,
                Player2Difficulty = inputModel.Player2Difficulty,
                Player1Heuristics = GetHeuristicIds(inputModel.Player1Heuristics).ToList(),
                Player2Heuristics = GetHeuristicIds(inputModel.Player2Heuristics).ToList()
            };

            return newGameInputModel;
        }

        public static FindNextMoveInputModel MapFindMoveInputModel(FindMoveInputModel inputModel)
        {
            var findMoveInputModel = new FindNextMoveInputModel()
            {
                GameId = Int32.Parse(inputModel.GameId),
                FullMove = inputModel.LastMove,
                TimeRemaining = inputModel.RemainingTime
            };

            return findMoveInputModel;
        }

        private static IEnumerable<int> GetHeuristicIds(string heuristicList)
        {
            if (heuristicList == null)
            {
                return new List<int>();
            }

            return heuristicList.Split(',').ToList().Cast<int>();
        }
    }
}